# extra

import os
import logging
import shutil

from utils.common import load_logging_config,dump_json
from consta import path_sep

def add_data_preprocessor():
    name='dp1_no_behavior'
    file='data_preprocessors.dp1'
    if os.path.exists(file):
        if not os.path.abspath(file).startswith(os.path.abspath('data_preprocessors')):
            target_dp_py=f'data_preprocessors/{os.path.splitext(file.split("/")[-1])[0]}.py'
            if os.path.basename(target_dp_py) in os.listdir('data_preprocessors'):
                logging.error('数据预处理模块重名')
            shutil.copy(file,target_dp_py)
            modulename=target_dp_py[:-3].replace('/','.')
        else:
            modulename=os.path.relpath(file,'.').replace(path_sep,'.')
            modulename=modulename if not modulename.endswith('.py') else modulename[:-3]
    elif file!='':
        modulename=file if not file.endswith('.py') else file[:-3]
        modulename=modulename.replace('/','.')
    else:
        modulename='data_preprocessors.dp1'
    classname='padding_no_extra'
    parameters='(1600, 200, 1),64'
    attribute=''

    data_preprocessor_json={
        'name':name,
        'modulename':modulename,
        'classname':classname,
        'parameters':parameters,
        'attribute':attribute
    }

    if not f'{data_preprocessor_json["name"]}.json' in os.listdir('jsons/data_preprocessors'):
        dump_json(data_preprocessor_json,f'jsons/data_preprocessors/{data_preprocessor_json["name"]}.json')
    else:
        logging.error('数据预处理器json文件重名，请重命名')

if __name__=='__main__':
    load_logging_config()
    add_data_preprocessor()